package com.baozhun.mall.common.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.baozhun.mall.common.R
import com.baozhun.mall.common.base.BaseApplication
import com.blankj.utilcode.util.SizeUtils

/**
 * @Title：通用适配器缺省view
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/29
 * @Version：1.0.0
 */
class EmptyView(context: Context) : LinearLayout(context) {

    private var mMarginTop: Float = 105f  //view距离顶部的高度，105f

    private var mEmptyHintText = BaseApplication.mInstance.getString(com.baozhun.mall.res.R.string.no_data)  //提示文字
    private var mEmptyHintTextSize = 13f  //提示文字大小
    private var mEmptyHintTextColor = com.baozhun.mall.res.R.color.color_999999  //提示文字颜色

    private var mEmptyDefaultImage = com.baozhun.mall.res.R.drawable.ic_data_empty

    private var mShowActionBt = false  //是否显示操作bt
    private var mHideEmptyImage = false  //是否显示缺省图
    private var mActionBtText = ""  //bt文字
    private var mActionBtTextSize = 16f  //bt文字大小
    private var mActionBtTextColor = com.baozhun.mall.res.R.color.color_999999   //bt文字颜色
    private var mBtAction: (() -> Unit)? = null   //bt事件回调

    private fun initView() {
        if (!mHideEmptyImage) {
            addView(buildEmptyImageView())
        }
        addView(buildHintTextView())
        if (mShowActionBt) {
            addView(buildActionBt())
        }
        gravity = Gravity.CENTER_HORIZONTAL
        orientation = VERTICAL
    }

    /**
     * 添加提示文字
     */
    private fun buildHintTextView(): TextView {
        var hintTv = TextView(context)
        hintTv.text = mEmptyHintText
        hintTv.textSize = mEmptyHintTextSize
        hintTv.setTextColor(ContextCompat.getColor(context, mEmptyHintTextColor))
        hintTv.layoutParams = MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
            setMargins(0, SizeUtils.sp2px(if (mHideEmptyImage) mMarginTop else 3f), 0, 0)
        }
        return hintTv
    }

    private fun buildEmptyImageView(): ImageView {
        var imageView = ImageView(context)
        imageView.setImageResource(mEmptyDefaultImage)
        imageView.layoutParams = MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
            setMargins(0, SizeUtils.sp2px(mMarginTop), 0, 0)
        }
        return imageView
    }

    /**
     * 添加操作的button
     */
    private fun buildActionBt(): TextView {
        var actionBt = TextView(context)
        actionBt.text = mActionBtText
        actionBt.textSize = mActionBtTextSize
        actionBt.setTextColor(ContextCompat.getColor(context, mActionBtTextColor))
        actionBt.gravity = Gravity.CENTER
        actionBt.layoutParams = MarginLayoutParams(LayoutParams.WRAP_CONTENT, SizeUtils.dp2px(42f)).apply {
            setMargins(0, SizeUtils.sp2px(10f), 0, 0)
        }
        actionBt.setPadding(SizeUtils.dp2px(35f), 0, SizeUtils.dp2px(35f), 0)
        actionBt.setOnClickListener {
            mBtAction?.invoke()
        }
        //设置stroke
        var gradientDrawable = GradientDrawable()
        gradientDrawable.shape = GradientDrawable.RECTANGLE
        gradientDrawable.setStroke(SizeUtils.dp2px(1f), ContextCompat.getColor(context, mActionBtTextColor))
        gradientDrawable.setColor(Color.TRANSPARENT)
        actionBt.background = gradientDrawable
        return actionBt
    }

    class Builder(context: Context) {
        private var mContext: Context = context
        private var vEmptyView = EmptyView(context)

        /**
         * 距离顶部的高度
         */
        fun marginTop(marginTop: Float): Builder {
            vEmptyView.mMarginTop = marginTop
            return this
        }

        /**
         * 缺省提示文字
         */
        fun emptyHintText(@StringRes resId: Int): Builder {
            vEmptyView.mEmptyHintText = mContext.getString(resId)
            return this
        }

        /**
         * 缺省提示文字大小
         */
        fun emptyHintTextSize(textSize: Int): Builder {
            vEmptyView.mEmptyHintTextSize = textSize.toFloat()
            return this
        }

        /**
         * 默认图片
         */
        fun emptyImage(imageRes: Int): Builder {
            vEmptyView.mEmptyDefaultImage = imageRes
            return this
        }

        /**
         * 缺省提示文字颜色
         */
        fun emptyHintTextColor(@ColorRes resId: Int): Builder {
            vEmptyView.mEmptyHintTextColor = resId
            return this
        }

        /**
         * button文本
         */
        fun actionBtText(@StringRes resId: Int): Builder {
            vEmptyView.mActionBtText = mContext.getString(resId)
            return this
        }

        /**
         * button文本大小
         */
        fun actionBtTextSize(textSize: Int): Builder {
            vEmptyView.mActionBtTextSize = textSize.toFloat()
            return this
        }

        /**
         * button文本颜色
         */
        fun actionBtTextColor(@ColorRes resId: Int): Builder {
            vEmptyView.mActionBtTextColor = resId
            return this
        }

        /**
         * 显示操作按钮
         */
        fun showActionBt(): Builder {
            vEmptyView.mShowActionBt = true
            return this
        }

        fun hideEmptyImageView(): Builder {
            vEmptyView.mHideEmptyImage = true
            return this
        }

        /**
         * button事件
         */
        fun btAction(action: () -> Unit): Builder {
            vEmptyView.mBtAction = action
            return this
        }

        fun build(): EmptyView {
            return vEmptyView.apply {
                //模拟网络请求时间，实现刚进入页面时不显示缺省图
                vEmptyView.postDelayed({
                    initView()
                },1000)
            }
        }
    }
}